﻿using Kingdee.BOS.WebApi.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;

namespace KingdeeViewBillDemo
{
    public delegate void setDgdata(string BillNo);
    public partial class QueryForm : Form
    {
       
        public QueryForm()
        {
            InitializeComponent();
        }
        public event setDgdata setReturnBillno;
        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        
        private void button1_Click(object sender, EventArgs e)
        {
            QueryBillList("5202-2008050001");
        }

        public void QueryBillList(string FbillNo)
        {

            K3CloudApiClient client = ClientCreate.getClient();
            String loginResult = client.ValidateLogin("60d1833eb4c418", "Administrator", "Bonsen1288", 2052);
            List<List<object>> listStr = null;
            if (textBox1.Text!="")
            {
                listStr = client.ExecuteBillQuery("{\"FormId\":\"SAL_SaleOrder\",\"FieldKeys\":\"FCUSTID,FBILLNO,FMATERIALID,FMATERIALNAME\",\"FilterString\":\"FBILLNO='" + textBox1.Text + "'\",\"OrderString\":\"\",\"TopRowCount\":0,\"StartRow\":0,\"Limit\":20}");
            }
            else
            {
                listStr = client.ExecuteBillQuery("{\"FormId\":\"SAL_SaleOrder\",\"FieldKeys\":\"FCUSTID,FBILLNO,FMATERIALID,FMATERIALNAME\",\"FilterString\":\"\",\"OrderString\":\"\",\"TopRowCount\":0,\"StartRow\":0,\"Limit\":20}");
            }
            
            string[] strArray = new string[4];
            List<string[]> strArrays = new List<string[]>();

            DataTable dt = new DataTable();
            dt.Columns.Add("客户ID");
            dt.Columns.Add("订单编号");
            dt.Columns.Add("物料ID");
            dt.Columns.Add("物料编号");

            DataRow dr = null;
            object[] otemp = new object[4];
            for (int i=0;i<listStr.Count;i++)
            {
                var temp = listStr[i];
                for(int j=0;j<temp.Count;j++)
                {
                    otemp[0] = temp[0];
                    otemp[1] = temp[1];
                    otemp[2] = temp[2];
                    otemp[3] = temp[3];
                }
                dr = dt.NewRow();
                dr.ItemArray = otemp;
                dt.Rows.Add(dr);
            }

            dataGridView1.DataSource = dt;
        }

        private void QueryForm_Load(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
           string Billnostr= this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
            string billNoreturn = Billnostr;
            setReturnBillno(billNoreturn);

            this.Hide();

        }
    }
}
